<script setup lang="ts">
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import router from "@/router";
import { onBeforeUnmount, reactive, ref, shallowRef } from "vue";
import { addNoticeApi, getNoticeList, getResidentialList } from "@/api/property";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { ElNotification } from "element-plus";
import "@wangeditor/editor/dist/css/style.css";

defineOptions({
  name: "noteList"
});
const form = reactive({
  name: ""
});
const loading = ref(false);
const residentialList = ref([]);
const residential = ref(-1);
const dataList = ref([]);
const addNotice = ref(false);
const editorRef = shallowRef();
const data = reactive({
  residentialId: residential,
  content: ""
});
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;
  editor.destroy();
});
const toolbarConfig: any = { excludeKeys: "fullScreen" };
const editorConfig = { placeholder: "请输入内容..." };
const mode = ref("default");

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};
const onSearch = () => {
  data.residentialId = residential.value;
  getNoticeList(residential.value).then(res => {
    dataList.value = res.data;
  });
};
const initData = () => {
  getResidentialList().then(res => {
    residentialList.value = res.data.records;
  });
  loading.value = false;
};
const AddNotice = () => {
  addNoticeApi(data).then(res => {
    console.log(data);
    if (res.code === 200)
      ElNotification({
        title: "Success",
        message: res.msg,
        type: "success"
      });
    else
      ElNotification({
        title: "Error",
        message: res.msg,
        type: "error"
      });
    onSearch();
  });
};
initData();
</script>

<template>
  <div class="main">
    <el-form
      ref="formRef"
      :inline="true"
      :model="form"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="公告内容：" prop="name">
        <el-input
          v-model="form.name"
          placeholder="请输入公告内容"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
          重置
        </el-button>
        <el-button
          type="primary"
          @click="addNotice=true"
        >发布公告
        </el-button>
      </el-form-item>
      <br />
      <el-form-item label="归属小区">
        <el-radio-group
          v-model="residential"
          size="small"
          @change="onSearch();data.residentialId=residential"
        >
          <el-radio-button
            :label="-1"
            :value="-1"
          >公开
          </el-radio-button
          >
          <el-radio-button
            v-for="list in residentialList"
            :label="list.id"
            :value="list.id"
          >{{ list.name }}
          </el-radio-button
          >
        </el-radio-group>
      </el-form-item>
    </el-form>
    <el-table :data="dataList.records" table-layout="fixed">
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="content" label="公告内容" />
      <el-table-column prop="created" label="发布时间" />
      <el-table-column prop="operatorId" label="发布人" />
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button
            type="danger"
            size="small"
            @click="deleteNotice(scope.row.id)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 100%">
      <el-pagination
        style="float: right"
        background
        layout="prev, pager, next"
        :total="dataList.total"
        :page-count="dataList.current"
      />
    </div>
    <div>
      <!--      使用Dialog弹窗发布公告-->
      <el-dialog v-model="addNotice" title="发布公告" width="500" center>
        <Toolbar
          style="border-bottom: 1px solid #ccc"
          :editor="editorRef"
          :defaultConfig="toolbarConfig"
          :mode="mode"
        />
        <Editor
          style="height: 500px; width: 100%"
          v-model="data.content"
          :defaultConfig="editorConfig"
          :mode="mode"
          @onCreated="handleCreated"
        />
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="addNotice = false">取消</el-button>
            <el-button type="primary"
                       @click="AddNotice();addNotice = false">
              确定
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<style scoped lang="scss">

</style>
